<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">

</head>

<body>
    <div id="compute">
        <div>
            <p>firstname:
                <input v-model="firstname" type="text" />
            </p>
            <p>lastname:
                <input v-model="lastname" type="text" />
            </p>
            <p style="font-weight:bold;">fullname: {{fullname}}</p>
            <button v-on:click="set">fullname=迈克尔 杰克逊</button>
        </div>
        <hr>
    </div>
    <div id="watch">
       <p>问题：<input type="text" v-model="question"></p>
       <p>答案：{{answer}}</p>
    </div>

    <script src="vue.js"></script>
    <script src="https://cdn.bootcss.com/lodash.js/4.17.5/lodash.core.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
    <script>
        new Vue({
            el: "#compute",
            data: {
                firstname: "li",
                lastname: "xiangwei"
            },
            methods: {
                set: function () {
                    this.fullname = "迈克尔 杰克逊";
                }
            },
            computed: {
                // fullname: function() {  //计算属性  默认有getter函数
                //    return this.firstname + " " + this.lastname;
                // }
                fullname: {
                    get: function () {
                        return this.firstname + " " + this.lastname;
                    },
                    set: function (name) {
                        var arr = name.split(' ');
                        this.firstname = arr[0];
                        this.lastname = arr[1];
                    }
                }
            }
        });

        var vm = new Vue({
            el: "#watch",
            data: {
                question: "",
                answer: "问题为空，请输入问题"
            },
            watch: {
                question: function () { //如果question发生变化，则运行此函数
                    this.answer = "问题正在输入。。。";
                    console.log(1);
                    // this.getAnswer();
                }
            },
            methods: {
                getAnswer: _.debounce(
                    function () {
                        if (this.question.indexOf('?') === -1) {
                            this.answer = "问题要用？结束";
                            return
                        }
                        this.answer = '思考中...';
                        axios.get('https://yesno.wtf/api')
                            .then(function (response) {
                                vm.answer = _.capitalize(response.data.answer)
                            })
                            .catch(function (error) {
                                vm.answer = 'Error! Could not reach the API. ' + error
                            });
                        console.log("发送请求");
                    },
                    500 //用户停止输入等待的毫秒数
                )
            }
        });
    </script>
</body>

</html>